//
//  NQiuzhiFirstStepViewController.m
//  nncloud
//
//  Created by macfai on 2018/12/14.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NQiuzhiFirstStepViewController.h"
#import "NQiuzhiSecondStepViewController.h"
#import "GFAddressPicker.h"
#import "UICustomDatePicker.h"

@interface NQiuzhiFirstStepViewController ()<STPickerSingleDelegate,GFAddressPickerDelegate,UITextFieldDelegate>
@property (nonatomic, strong) GFAddressPicker *pickerView;

@property (weak, nonatomic) IBOutlet UITextField *nameTf;

@property (weak, nonatomic) IBOutlet UITextField *heightTf;

@property (weak, nonatomic) IBOutlet UITextField *weightTf;

@property (weak, nonatomic) IBOutlet UITextField *telTf;

@property (weak, nonatomic) IBOutlet UITextField *emailTf;
@property (weak, nonatomic) IBOutlet UILabel *genderLabel;
@property (weak, nonatomic) IBOutlet UILabel *birthLael;
@property (weak, nonatomic) IBOutlet UILabel *marriageLabel;
@property (weak, nonatomic) IBOutlet UILabel *reigion;

@property (nonatomic,strong)STPickerSingle *picker;

@end

@implementation NQiuzhiFirstStepViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    [self.navigationItem setItemWithTitle:@"求职信息" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 35) title:@"下一步" Image:nil selectedIMG:nil tartget:self action:@selector(next)];
    [self addAction];
    
}

- (void)addAction{
    
    //添加textField代理
    self.nameTf.delegate = self;
    self.heightTf.delegate = self;
    self.weightTf.delegate =self;
    self.telTf.delegate = self;
    self.emailTf.delegate = self;
    ///添加各个手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseGender:)];
//    [tap view].tag=500;
////    singleTapView1.tag = 500;
    [self.genderLabel addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(choosebirth:)];
//    UIView *singleTapView2 = [tap view];
//    singleTapView2.tag = 501;
    [self.birthLael addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseMarriage:)];
//    UIView *singleTapView3 = [tap view];
//    singleTapView3.tag = 502;
    [self.marriageLabel addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseRegion:)];
//    UIView *singleTapView4 = [tap view];
//    singleTapView4.tag = 503;
    [self.reigion addGestureRecognizer:tap3];
}


- (void)chooseGender:(UITapGestureRecognizer *)tap
{
    //隐藏键盘
    [self.view endEditing:YES];
    
    YYLog(@"选择性别");
    NSMutableArray *data = [NSMutableArray arrayWithArray:@[@"男",@"女",@"不限"]];
    STPickerSingle *single = [[STPickerSingle alloc]init];
    single.tag = 121;
    [single setArrayData:data];
    [single setTitleUnit:@""];
    [single setDelegate:self];
    [single show];
    
}


- (void)chooseRegion:(UITapGestureRecognizer *)tap
{
    
    //隐藏键盘
    [self.view endEditing:YES];
    YYLog(@"选择地区");
    //地区选择器
    self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    [self.pickerView updateAddressAtProvince:@"北京市" city:@"北京市" town:@"东城区"];
    self.pickerView.delegate = self;
    self.pickerView.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:self.pickerView];
    
    
}

- (void)chooseMarriage:(UITapGestureRecognizer *)tap
{
    
    YYLog(@"选择m");
    NSMutableArray *data = [NSMutableArray arrayWithArray:@[@"已婚",@"未婚",@"其他"]];
    STPickerSingle *single = [[STPickerSingle alloc]init];
    single.tag = 122;
    [single setArrayData:data];
    [single setTitleUnit:@""];
    [single setDelegate:self];
    [single show];
    
}
- (void)choosebirth:(UITapGestureRecognizer *)tap
{
    YYLog(@"选择s");
    //时间选择器日期选择器
    [UICustomDatePicker showCustomDatePickerAtView:self.view choosedDateBlock:^(NSDate *date) {
        
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        NSString *string = [fmt stringFromDate:date];
        self.birthLael.text =  string;
        YYLog(@"current Date:%@",string);
        [NGlobalModel shareInstance].birth = string;
    } cancelBlock:^{
        
    }];
    
    
}
#pragma mark - 地区选择器delegate

- (void)GFAddressPickerCancleAction
{
    [self.pickerView removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    
    [self.pickerView removeFromSuperview];
    self.reigion.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
    [NGlobalModel shareInstance].region = self.reigion.text;
    NSLog(@"%@  %@  %@",province,city,area);
}
#pragma - STpickerdelegate
///带索引值的
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle selectIndex:(NSInteger)selectIndex{
    
    
    switch (pickerSingle.tag) {
        case 121:
        {
            self.genderLabel.text = selectedTitle;
            [NGlobalModel shareInstance].gender = [NSString stringWithFormat:@"%ld",selectIndex];
        }
            break;
            
        case 122:
        {
            self.marriageLabel.text = selectedTitle;
            [NGlobalModel shareInstance].marriage = [NSString stringWithFormat:@"%ld",selectIndex];
        }
            break;
        default:
            break;
    }
    
}


///不带索引值的只能拿到标题，这里需要选择后向后台传递一个index  所以扩展了出了上面那个代理
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle{
    
    switch (pickerSingle.tag) {
        case 121:
            {
                self.genderLabel.text = selectedTitle;
                [NGlobalModel shareInstance].gender = selectedTitle;
            }
            break;
            
        case 122:
        {
            self.marriageLabel.text = selectedTitle;
             [NGlobalModel shareInstance].marriage = selectedTitle;
        }
            break;
        default:
            break;
    }
}


#pragma mark - textField delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField==self.nameTf) {
        
        [NGlobalModel shareInstance].name = textField.text;
        
    }else if (textField==self.weightTf){
        
       
        [NGlobalModel shareInstance].weight = textField.text;
       
        
    }else if (textField==self.heightTf){
        
        [NGlobalModel shareInstance].height = textField.text;
        
    }else if (textField==self.telTf){
        
        if (![ToolKit validateMobile:self.telTf.text]) {
            [AFToast showText:@"请输入合法的电话号码"];
            self.telTf.text = @"";
            return;
        }else{
            
            [NGlobalModel shareInstance].phone = textField.text;
        }
        
    }else{
        
        if (![ToolKit validateMail:self.emailTf.text]) {
            [AFToast showText:@"请输入合法的邮箱"];
            return;
        }else{
            //电邮
            [NGlobalModel shareInstance].email = textField.text;
        }
    }
}

#pragma mark - 字数限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.nameTf) {
        //这里的if为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.nameTf.text.length >= 10) {
            self.nameTf.text = [textField.text substringToIndex:10];
            return NO;
        }
    }else if (textField == self.heightTf) {
        
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.heightTf.text.length >= 3) {
            self.heightTf.text = [textField.text substringToIndex:3];
            return NO;
        }
    }else if (textField == self.weightTf) {
        //这里的if为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.weightTf.text.length >= 3) {
            self.weightTf.text = [textField.text substringToIndex:3];
            return NO;
        }
    }
    
    return YES;
    
}

#pragma mark - 下一步

- (void)next{
    
    if (self.nameTf.text.length==0||self.telTf.text.length==0) {
        [AFToast showText:@"请输入必填项"];
        return;
    }
    NQiuzhiSecondStepViewController *sec = [NQiuzhiSecondStepViewController new];
    [self.navigationController pushViewController:sec animated:YES];
}

@end
